/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.server.dao.registration;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.dto.MstRoomDTO;
import vn.smartware.co.dto.MstRoomPriceDTO;
import vn.smartware.co.forms.RegisterRoomForm;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.java.net.commons.Utils;
import vn.smartware.co.server.dao.factory.AbstractRegisterRoomDAO;
import vn.smartware.co.server.sql.factory.AbstractRegisterRoomSQLManager;
import vn.smartware.co.server.sql.factory.SQLFactory;

/**
 * @author thuctt
 * RegisterRoomDAO
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class RegisterRoomDAO extends AbstractRegisterRoomDAO {

	private static Log log = LogFactory.getLog(RegisterRoomDAO.class);
	
	/**
	 * get room list
	 * @param form 
	 * @param user 
	 * @param conn
	 * @return ArrayList
	 * @throws ServerException
	 */
	public ArrayList getRooms(RegisterRoomForm form, LoginDTO user,
			Connection conn) throws ServerException {
		
		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		ResultSet rs = null;
		int count = 0;
		ArrayList roomList = new ArrayList();
		ArrayList priceList = new ArrayList();
		MstRoomDTO room = new MstRoomDTO();
		try {
			manager.getRoomsSQL(form, user, conn);
			rs = manager.executeQuery();
			String preRoomNo = "";
			
			while (rs.next()){
				String roomNo = rs.getString("ROOM_NO");
				
				if (!Utils.isNullEmpty(preRoomNo) && !roomNo.equals(preRoomNo)){
					room.setPriceList(priceList);
					roomList.add(room);
					room = new MstRoomDTO();
					priceList = new ArrayList();
				}
				
				room.setRoomNo(roomNo);
				room.setRoomName(rs.getString("ROOM_NAME"));
				room.setRoomType(rs.getString("ROOM_TYPE"));
				room.setRoomTypeName(rs.getString("ROOM_TYPE_NAME"));
				room.setStatus(rs.getString("STATUS"));
				room.setStatusName(rs.getString("STATUS_NAME"));
				room.setDescription(rs.getString("DESCRIPTION"));
				
				String priceNo = Utils.toEmpty(rs.getString("PRICE_NO"));
				if (!Utils.isNullEmpty(priceNo)){
					MstRoomPriceDTO price = new MstRoomPriceDTO();
					price.setPriceNo(rs.getString("PRICE_NO"));
					price.setRentType(rs.getString("RENT_TYPE"));
					price.setRentTypeName(rs.getString("RENT_TYPE_NAME"));
					price.setPrice(rs.getString("PRICE"));
					price.setMoneyType(rs.getString("MONEY_TYPE"));
					price.setMoneyTypeName(rs.getString("MONEY_TYPE_NAME"));
					priceList.add(price);
				}
				
				preRoomNo = roomNo;
				count++;
			}
			// last room
			if (count > 0){
				room.setPriceList(priceList);
				roomList.add(room);
			}
			
			return roomList;
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * deletePriceList
	 * @param roomNo
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void deletePriceList(String roomNo, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		int count = 0;
		try {
			manager.makeDeletePriceListSQL(roomNo, user, conn);
			count = manager.executeUpdate();
			log.info("deletePriceList: " + count + " rows deleted.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * deleteRoom
	 * @param room
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void deleteRoom(MstRoomDTO room, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		int count = 0;
		try {
			manager.makeDeleteRoomSQL(room.getRoomNo(), user, conn);
			count = manager.executeUpdate();
			log.info("deleteRoom: " + count + " rows deleted.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * registerPrice
	 * @param roomNo
	 * @param price
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void registerPrice(String roomNo, MstRoomPriceDTO price, LoginDTO user,
			Connection conn) throws ServerException {

		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		int count = 0;
		try {
			manager.makeRegisterPriceSQL(roomNo, price, user, conn);
			count = manager.executeUpdate();
			log.info("registerPrice: " + count + " rows updated.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}

	/**
	 * registerRoom
	 * @param room
	 * @param user
	 * @param conn
	 * @return String
	 * @throws ServerException
	 */
	public String registerRoom(MstRoomDTO room, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		ResultSet rs = null;
		int count = 0;
		String roomNo = "";
		try {
			manager.makeRegisterRoomSQL(room, user, conn);
			count = manager.executeUpdate();
			log.info("registerRoom: " + count + " rows updated.");
			//get room no just registered
			manager = SQLFactory.createRegisterRoomSQLManager();
			manager.getMaxRoomNoSQL(conn);
			rs = manager.executeQuery();
			if (rs.next()){
				roomNo = rs.getString("ROOM_NO");
			}
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close(rs);
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
		return roomNo;
	}

	/**
	 * updateRoom
	 * @param room
	 * @param user
	 * @param conn
	 * @throws ServerException
	 */
	public void updateRoom(MstRoomDTO room, LoginDTO user, Connection conn)
			throws ServerException {

		AbstractRegisterRoomSQLManager manager = SQLFactory.createRegisterRoomSQLManager();
		int count = 0;
		try {
			manager.makeUpdateRoomSQL(room, user, conn);
			count = manager.executeUpdate();
			log.info("updateRoom: " + count + " rows updated.");
			
		} catch (SQLException e) {
			log.error("SQLException: ", e);
			throw new ServerException(e.getMessage());
			
		} finally {
			try {
				manager.close();
				
			} catch (SQLException e) {
				log.error("SQLException: ", e);
				throw new ServerException(e.getMessage());
			}
		}
	}
	
}
